package com.springboot.file;

import cn.hutool.core.util.StrUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Classname FileStoreServiceUtil
 * @Description TODO
 * @Version 1.0.0
 * @Date 2024/2/22 14:39
 * @Created by zyj
 */
@Component
@Slf4j
public class FileStoreServiceUtil {

    @Resource
    private BaseFileStoreFileUtil baseFileStoreFileUtil;

    /**
     * img标签正则表达式 不区分大小写
     **/
    private final static Pattern PATTERN_IMG_TAG = Pattern.compile("<\\s*img\\s+([^>]*)\\s*", Pattern.CASE_INSENSITIVE);
    /**
     * src属性值正则表达式 不区分大小写
     **/
    private final static Pattern PATTERN_SRC_ATTRIB = Pattern.compile("src\\s*=\\s*['\"]([^'\"]+)['\"]", Pattern.CASE_INSENSITIVE);

    /**
     * 动态替换签名访问地址为未签名url(仅处理当前环境文件链接)
     *
     * @param html 输入的HTML字符串
     */
    public String replaceUrlUnsignedUrl(String html) {
        log.info("原始字符串为:" + html);
        Matcher matcherForTag = PATTERN_IMG_TAG.matcher(html);
        StringBuffer sb = new StringBuffer();
        boolean result = matcherForTag.find();
        while (result) {
            StringBuffer sbreplace = new StringBuffer("<img ");
            Matcher matcherForAttrib = PATTERN_SRC_ATTRIB.matcher(matcherForTag.group(1));
            if (matcherForAttrib.find()) {
                String attributeStr = matcherForAttrib.group(1);
                //当前环境url才做替换
                if (baseFileStoreFileUtil.isCurrentEnvUrl(attributeStr)) {
                    String attributeStrReplace = StrUtil.replace(attributeStr, baseFileStoreFileUtil.getDomain(), "");
                    matcherForAttrib.appendReplacement(sbreplace, "src=\"" + attributeStrReplace + "\"");
                }
            }
            matcherForAttrib.appendTail(sbreplace);
            matcherForTag.appendReplacement(sb, sbreplace.toString());
            result = matcherForTag.find();
        }
        matcherForTag.appendTail(sb);
        String strReplace = sb.toString();
        log.info("替换后字符串为:" + strReplace);
        return strReplace;
    }

    /**
     * 动态添加访问地址为未签名url(仅处理当前环境文件链接)
     *
     * @param html 输入的HTML字符串
     */
    public String addUrlUnsignedUrl(String html) {
        log.info("原始字符串为:" + html);
        Matcher matcherForTag = PATTERN_IMG_TAG.matcher(html);
        StringBuffer sb = new StringBuffer();
        boolean result = matcherForTag.find();
        while (result) {
            StringBuffer sbreplace = new StringBuffer("<img ");
            Matcher matcherForAttrib = PATTERN_SRC_ATTRIB.matcher(matcherForTag.group(1));
            if (matcherForAttrib.find()) {
                String attributeStr = matcherForAttrib.group(1);
                //根据类型获取替换后的属性值
                String attributeStrReplace = baseFileStoreFileUtil.getDomain()+attributeStr;
                matcherForAttrib.appendReplacement(sbreplace, "src=\"" + attributeStrReplace + "\"");
            }
            matcherForAttrib.appendTail(sbreplace);
            matcherForTag.appendReplacement(sb, sbreplace.toString());
            result = matcherForTag.find();
        }
        matcherForTag.appendTail(sb);
        String strReplace = sb.toString();
        log.info("替换后字符串为:" + strReplace);
        return strReplace;
    }



}
